package logic

import (
	"context"
	"day3/models/mysqlinit"
	"fmt"

	"day3/Realrpc/internal/svc"
	"day3/Realrpc/realrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *realrpc.LoginRequest) (*realrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user mysqlinit.User
	mysqlinit.DB.Where("account = ?", in.Account).Find(&user)
	if user.ID == 0 {
		m := mysqlinit.User{

			Account:  in.Account,
			Password: in.Password,
		}
		mysqlinit.DB.Debug().Create(&m)
	} else {
		if user.Password != in.Password {
			return nil, fmt.Errorf("密码错误，请重新输入")
		}
	}

	return &realrpc.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
